#include <e.h>
#include "e_mod_main.h"

struct _E_Config_Dialog_Data
{
   /*- BASIC -*/
   Evas_List *ilist;
   char *if_name;
   /*- ADVANCED -*/
   Evas_List *tlist;
   char *theme;
   int alpha;
   /*- GUI -*/
   
   struct {
      Evas_Object *dlist, *tlist;
   } gui;
   E_Config_Dialog *cfd;
   /*- UTILS -*/
   Widget *w;
};

typedef struct _E_Mod_Theme_Data E_Mod_Theme_Data;
typedef struct _E_Mod_Iface_Data E_Mod_Iface_Data;

struct _E_Mod_Theme_Data
{
   E_Config_Dialog *cfd;
   char *theme;
   char *file;
   void *data;
   Evas_Object *text;
};

struct _E_Mod_Iface_Data
{
   E_Config_Dialog *cfd;
   char *if_name;
   void *data;
};











/* PROTOTYPES - same all the time */
static void *_create_data(E_Config_Dialog *cfd);
static void _free_data(E_Config_Dialog *cfd, E_Config_Dialog_Data *cfdata);
static int _basic_apply_data(E_Config_Dialog *cfd, E_Config_Dialog_Data *cfdata);
static int _advanced_apply_data(E_Config_Dialog *cfd, E_Config_Dialog_Data *cfdata);
static Evas_Object *_basic_create_widgets(E_Config_Dialog *cfd, Evas *evas, E_Config_Dialog_Data *cfdata);
static Evas_Object *_advanced_create_widgets(E_Config_Dialog *cfd, Evas *evas, E_Config_Dialog_Data *cfdata);
static void _ilist_cb_change(void *data);
static void _tlist_cb_change(void *data);
static void _fill_data(E_Config_Dialog_Data *cfdata);

/* a nice easy setup function that does the dirty work */
EAPI E_Config_Dialog *
_net_widget_config(Widget *w)
{
	E_Config_Dialog *cfd;
	E_Config_Dialog_View *v;
	E_Container *con;
	char buf[4096];
	if (!w)
   		return NULL;
	   
	v = E_NEW (E_Config_Dialog_View, 1);
  	if (v == NULL)
    {
      warn ("malloc()");
      return;
    }
   
   /* methods */
	v->create_cfdata           = _create_data;
	v->free_cfdata             = _free_data;
	v->basic.apply_cfdata      = _basic_apply_data;
	v->basic.create_widgets    = _basic_create_widgets;
	v->advanced.apply_cfdata   = _advanced_apply_data;
	v->advanced.create_widgets = _advanced_create_widgets;
	/* create config diaolg for NULL object/data */
	snprintf (buf, sizeof (buf), "%s/module.edj",e_module_dir_get (w->m));
  	con = e_container_current_get (e_manager_current_get ());
	cfd = e_config_dialog_new(con,
			     _("e17.net.module Settings"),
			    "E", "e17.net.module",
			     buf, 0, v, w);
   return cfd;
}
/**--CREATE--**/

static void *
_create_data(E_Config_Dialog *cdd)
{
   /* Create cfdata - cfdata is a temporary block of config data that this
    * dialog will be dealing with while configuring. it will be applied to
    * the running systems/config in the apply methods
    */
   Widget *w; 
   E_Config_Dialog_Data *cfdata;
   
   if (!cdd)
    return NULL;

  w = cdd->data;
  if (!w)
  	return NULL;
   cfdata = E_NEW(E_Config_Dialog_Data, 1);
   cfdata->cfd=cdd;
   cfdata->w=w;
   _fill_data(cfdata);
   return cfdata;
}

static void
_fill_data(E_Config_Dialog_Data *cfdata)
{
	E_Mod_Iface_Data *idata;
	if (!cfdata)
		return;
	cfdata->if_name=evas_stringshare_add(cfdata->w->conf->iface);
	/*Fill List of Ifaces */
	struct ifaddrs *ifap, *ifa;
	struct if_data *ifd;
	if (getifaddrs(&ifap) < 0)
		return;
	for (ifa = ifap; ifa; ifa = ifa->ifa_next) 
	{
		if (ifa->ifa_data)
		{
			idata=E_NEW(E_Mod_Iface_Data,1);
			idata->if_name=evas_stringshare_add(ifa->ifa_name);
			idata->cfd=cfdata->cfd;
			cfdata->ilist=evas_list_append(cfdata->ilist,idata);
//			printf("added %s\n",idata->if_name);
		}
	}	
	freeifaddrs(ifap);
	
	Ecore_List *themes;
	char *themefile;
	const char *themename=NULL;
	const char fulltheme[PATH_MAX];
	const char *e_mod_themes_dir;
	char *a;
	char buf[PATH_MAX];
	Widget *w;
	Ecore_Evas *eebuf;
    Evas *evasbuf;
	Evas_Object *o2 = NULL;
	eebuf = ecore_evas_buffer_new(1, 1);
    evasbuf = ecore_evas_get(eebuf);
    o2 = edje_object_add(evasbuf);
	w=cfdata->w;
	

	snprintf (buf, sizeof (buf), "%s/%s",e_module_dir_get (w->m),"themes");
	e_mod_themes_dir=evas_stringshare_add(buf);
	if (ecore_file_is_dir(e_mod_themes_dir))
		themes = ecore_file_ls(e_mod_themes_dir);
		
	while ((themefile = ecore_list_next(themes)))
	{
		snprintf(fulltheme, sizeof(fulltheme), "%s/%s", e_mod_themes_dir, themefile);
		if (ecore_file_is_dir(fulltheme))
			continue;
	    if (!edje_object_file_set(o2, fulltheme, "modules/e17.net.module/main"))
	    	continue;
		E_Mod_Theme_Data *tdata;	
		tdata = E_NEW(E_Mod_Theme_Data, 1);
		tdata->cfd = cfdata->cfd;
		tdata->file = evas_stringshare_add(fulltheme);
		tdata->theme = evas_stringshare_add(themefile);
		tdata->data = NULL;
		cfdata->tlist=evas_list_append(cfdata->tlist,tdata);
//		printf("%s\n",tdata->theme);
	}
	ecore_list_destroy(themes);
	evas_object_del(o2);
    ecore_evas_free(eebuf);
    evas_stringshare_del(e_mod_themes_dir);
    
   /*- ADVANCED -*/
   cfdata->theme=evas_stringshare_add(cfdata->w->conf->theme);
   cfdata->alpha=cfdata->w->conf->alpha;
}

static void
_free_data(E_Config_Dialog *cdd, E_Config_Dialog_Data *cfdata)
{
	Evas_List *tlist;
	Evas_List *ilist;
	E_Mod_Theme_Data *tdata;
	E_Mod_Iface_Data *idata;
	if (!cfdata)
		return;
	for (tlist = cfdata->tlist; tlist; tlist = tlist->next)
   	{	
		tdata=tlist->data;
		evas_stringshare_del(tdata->theme);
		evas_stringshare_del(tdata->file);
		E_FREE(tdata);
	}
//	evas_list_free(cfdata->tlist);
	
	for (ilist = cfdata->ilist; ilist; ilist = ilist->next)
   	{
		idata=ilist->data;
//		printf("deleted %s\n",idata->if_name);
		evas_stringshare_del(idata->if_name);
		E_FREE(idata);
	}	
//	evas_list_free(cfdata->ilist);
	if (cfdata)
    	{
    		//evas_stringshare_del(cfdata->current_theme);
    		//evas_stringshare_del(cfdata->theme);
    		//evas_stringshare_del(cfdata->if_name);
		}
	cfdata->w->config_dialog = NULL;
   
   
   
   
   
   /* Free the cfdata */
	E_FREE(cfdata);
}
/**--APPLY--**/


static int
_basic_apply_data(E_Config_Dialog *cdd, E_Config_Dialog_Data *cfdata)
{
   /* Actually take our cfdata settings and apply them in real life */
   Widget *w;
   w=cfdata->w;
   evas_stringshare_del(w->conf->iface);
   w->conf->iface=evas_stringshare_add(cfdata->if_name);
   w->ibytes=0;
   w->obytes=0;
   w->ipackets=0;
   w->opackets=0;
   w->link_state=0;
   e_config_save_queue();
   _net_widget_config_updated(w);
   return 1; /* Apply was OK */
}

static int
_advanced_apply_data(E_Config_Dialog *cfd, E_Config_Dialog_Data *cfdata)
{
	Widget *w;
   	w=cfdata->w;
	evas_stringshare_del(w->conf->iface);
	w->conf->iface=evas_stringshare_add(cfdata->if_name);
	evas_stringshare_del(w->conf->theme);
	w->conf->theme=evas_stringshare_add(cfdata->theme);
	w->conf->alpha = cfdata->alpha;
	e_config_save_queue();
	_net_widget_config_updated(w);
   	return 1; /* Apply was OK */
}

/**--GUI--**/
static Evas_Object *
_basic_create_widgets(E_Config_Dialog *cdd, Evas *evas, E_Config_Dialog_Data *cfdata)
{
	Evas_List *ilist;
	E_Mod_Iface_Data *idata;
	int i=0;
	int selnum=0;
/* generate the elements layout for a basic dialog */
/*
 * should be ilist for ifaces and nothing more
 */
   Evas_Object *o, *il, *of, *ot,*os,*ob;
   
   o = e_widget_table_add(evas, 0);
   
   of = e_widget_frametable_add(evas, D_("Network Interfaces"), 0);
   
	il = e_widget_ilist_add(evas, 100, 100, NULL);
    for (ilist = cfdata->ilist; ilist; ilist = ilist->next)
   	{
   		idata=ilist->data;
   		idata->data=cfdata;
   		e_widget_ilist_append(il, NULL,idata->if_name ,_ilist_cb_change, idata, NULL);	
   		if (strcmp(idata->if_name,cfdata->if_name)==0)
			selnum=i;
		i++;		
   	}
   e_widget_min_size_set(il, 180, 100);
   e_widget_ilist_go(il); 
   e_widget_frametable_object_append(of, il, 0, 0, 1, 2, 1, 1, 1, 1);
   if (selnum >= 0)
		e_widget_ilist_selected_set(il, selnum);
   e_widget_table_object_append(o, of, 0, 0, 1, 2, 1, 1, 1, 1);

	return o;
}

static Evas_Object *
_advanced_create_widgets(E_Config_Dialog *cfd, Evas *evas, E_Config_Dialog_Data *cfdata)
{
/* generate the elements layout for a advanced dialog */
/*	should be ilist for ifaces 
 * 	slider for alpha
 * 	ilist for theme files and textblock to preview theme data
 *
 *  
 **/

 
 	Evas_Object *o, *il,*il1, *of, *ot,*os,*ob;
	Evas_List *ilist,*tlist;
	E_Mod_Iface_Data *idata;
	E_Mod_Theme_Data *tdata;
	int i=0;
	int selnum=0;
 	o = e_widget_table_add(evas, 0);
   
   of = e_widget_frametable_add(evas, D_("Network Interfaces"), 0);
   
	il = e_widget_ilist_add(evas, 100, 100, NULL);
    for (ilist = cfdata->ilist; ilist; ilist = ilist->next)
   	{
   		
   		idata=ilist->data;
   		idata->data=cfdata;
//   		printf("%s\n",idata->if_name);
   		e_widget_ilist_append(il, NULL,idata->if_name ,_ilist_cb_change, idata, NULL);	
   		if (strcmp(idata->if_name,cfdata->if_name)==0)
			selnum=i;
		i++;		
   	}
   e_widget_min_size_set(il, 180, 100);
   e_widget_ilist_go(il); 
   e_widget_frametable_object_append(of, il, 0, 0, 1, 2, 1, 1, 1, 1);
   if (selnum >= 0)
		e_widget_ilist_selected_set(il, selnum);
   e_widget_table_object_append(o, of, 0, 0, 1, 2, 1, 1, 1, 1);
   
   of = e_widget_frametable_add(evas, D_("Themes"), 0);
   
   	i=0;selnum=0;
	il = e_widget_ilist_add(evas, 100, 100, NULL);
    for (tlist = cfdata->tlist; tlist; tlist = tlist->next)
   	{
   		
   		tdata=tlist->data;
   		tdata->data=cfdata;
//   		printf("%s\n",tdata->theme);
   		char *noext;
   		noext = ecore_file_strip_ext(tdata->theme);
   		
   		e_widget_ilist_append(il, NULL,noext ,_tlist_cb_change, tdata, NULL);	
   		if (strcmp(tdata->theme,cfdata->theme)==0)
			selnum=i;
		i++;
		E_FREE(noext);
   	}
   e_widget_min_size_set(il, 180, 100);
   e_widget_ilist_go(il); 
   e_widget_frametable_object_append(of, il, 0, 0, 1, 2, 1, 1, 1, 1);
   if (selnum >= 0)
		e_widget_ilist_selected_set(il, selnum);
   e_widget_table_object_append(o, of, 2,0 , 1, 2, 1, 1, 1, 1);
   
 	
   of = e_widget_frametable_add(evas, D_("Appearance"), 1);
   ob = e_widget_label_add(evas, D_("Alpha"));
   e_widget_frametable_object_append(of, ob, 0, 0, 2, 1, 1, 1, 1, 1);
   ob = e_widget_slider_add(evas, 1, 0, _("%1.0f"), 0.0, 255, 1.0, 0,
                            NULL, &(cfdata->alpha), 130);
   e_widget_frametable_object_append(of, ob, 0, 1, 3, 1, 1, 0, 1, 0);
   e_widget_table_object_append(o, of, 0, 2, 1, 2, 1, 1, 1, 1);
	return o;
}

static void
_ilist_cb_change(void *data)
{
	E_Mod_Iface_Data *idata;
	
   	E_Config_Dialog_Data *cfdata;
   	Evas_List *l;
	idata=data;
	cfdata=idata->data;
	if (strcmp(cfdata->if_name,idata->if_name)!=0)
	{
		evas_stringshare_del(cfdata->if_name);
		cfdata->if_name=evas_stringshare_add(idata->if_name);
	}
//   cfdata->if_name=

}

static void
_tlist_cb_change(void *data)
{
	E_Mod_Theme_Data *tdata;
	
   	E_Config_Dialog_Data *cfdata;
   	Evas_List *l;
	tdata=data;
	cfdata=tdata->data;
	if (strcmp(cfdata->theme,tdata->theme)!=0)
	{
		evas_stringshare_del(cfdata->theme);
		cfdata->theme=evas_stringshare_add(tdata->theme);
	}
//   cfdata->if_name=

}
